/*
 * @Author: zhang_wenjuan
 * @Date: 2020-12-24 08:44:36
 * @LastEditTime: 2021-08-18 16:26:18
 * @Description: 弹窗移动
 */

// v-dialogDrag: 弹窗拖拽
// Vue.directive('drag', {
export const drag = {
    bind(el) {
        console.log(el);
        const dialogHeaderEl = el
        const dragDom = el
        dialogHeaderEl.style.cursor = 'move'
        console.log(window.getComputedStyle(dragDom, null));
        // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
        const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
        dialogHeaderEl.onmousedown = (e) => {
            // 鼠标按下，计算当前元素距离可视区的距离
            const disX = e.clientX - dialogHeaderEl.offsetLeft
            const disY = e.clientY - dialogHeaderEl.offsetTop
            // 获取到的值带px 正则匹配替换
            let styL, styT

            // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
            if (sty.left.includes('%')) {
                styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
                styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
            } else {
                styL = +sty.left.replace(/\px/g, '')
                styT = +sty.top.replace(/\px/g, '')
            }
            const elCurrentWidth=Number(window.getComputedStyle(dragDom, null).width.replace(/\px/g, ''))
            const elCurrentheight=Number(window.getComputedStyle(dragDom, null).height.replace(/\px/g, ''))
            // 鼠标移动
            document.onmousemove = function (e) {
                // 通过事件委托，计算移动的距离
                var l = e.clientX - disX
                var t = e.clientY - disY
                // 移动当前元素
                // console.log(t);
                console.log(document.documentElement.clientHeight-elCurrentheight);
                // console.log(t>=document.documentElement.clientHeight);
                if (t>=document.documentElement.clientHeight-elCurrentheight) {
                    t=document.documentElement.clientHeight-elCurrentheight
                }
                if (t<=0) {
                    t=0
                }
                if (l>=document.documentElement.clientWidth-elCurrentWidth) {
                    l=document.documentElement.clientWidth-elCurrentWidth
                }
                if (l<=0) {
                    l=0
                }
                dragDom.style.left = `${l}px`
                dragDom.style.top = `${t}px`
                // 动态字符串反引号
                // 将此时的位置传出去
                // binding.value({x:e.pageX,y:e.pageY})
            }
            // 鼠标松开事件
            document.onmouseup = function (e) {
                document.onmousemove = null
                document.onmouseup = null
            }
        }
    }
}
// 输入框自动获取焦点
// Vue.directive('focus', {
export const focus = {
    bind(el) {
        // 自动获取焦点
        el.focus();
    }
}
// 输入框不能输入空格
// Vue.directive('noblank', {
export const noblank = {
    update: function (el, { value, modifiers }) {
        // 查询输入框不能输入空格
        try {
            el.children[0].value = el.children[0].value.replace(/\s/g, "")
            el.children[0].dispatchEvent(new Event(modifiers.lazy ? "change" : "input"))
        } catch (e) {
            // console.log(e)
        }
    }
}
